////////////////////////////////////////////////////////////////////////////////
//
//  The MIT License
//
//  Copyright (c) 2008 - 2009. Dimarik
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////

package ru.goodcore.logging {

import flash.events.EventDispatcher;

/**
 *
 *
 * @author				Dimarik
 * @version				1.0
 * @playerversion		9
 * @langversion			3.0
 */
public class LogLogger extends EventDispatcher implements ILogger {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor
	 *
	 * @param category
	 */
	public function LogLogger(category:String) {
		super();
		this._category = category;
	}

	//-------------------------------------------------------------------------
	//
	//   Properties
	//
	//-------------------------------------------------------------------------

	//---------------------------------
	//  category
	//---------------------------------
	/**
	 * @private
	 */
	private var _category:String;

	public function get category():String {
		return _category;
	}

	//-------------------------------------------------------------------------
	//
	//   Interface implementation: ILogger
	//
	//-------------------------------------------------------------------------

	public function log(level:int, msg:String, ... rest):void {
		if (!super.hasEventListener(LogEvent.LOG)) return;
		rest.unshift(msg);
		// replace all of the parameters in the msg string
		msg = this.substitute.apply(null, rest);
		super.dispatchEvent( new LogEvent(msg, level) );
	}

	public function debug(msg:String, ... rest):void {
		rest.unshift(LogEventLevel.DEBUG, msg);
		this.log.apply(null, rest);
	}

	public function error(msg:String, ... rest):void {
		rest.unshift(LogEventLevel.ERROR, msg);
		this.log.apply(null, rest);
	}

	public function fatal(msg:String, ... rest):void {
		rest.unshift(LogEventLevel.FATAL, msg);
		this.log.apply(null, rest);
	}

	public function info(msg:String, ... rest):void {
		rest.unshift(LogEventLevel.INFO, msg);
		this.log.apply(null, rest);
	}

	public function warn(msg:String, ... rest):void {
		rest.unshift(LogEventLevel.WARN, msg);
		this.log.apply(null, rest);
	}


	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	/**
	 * @private 
	 * @param message
	 * @param replaces
	 * @return 
	 * 
	 */
	private function substitute(message:String, ...replaces):String {
		for (var i:uint = 0; i < replaces.length; i++) {
			message = message.replace(new RegExp("%s" + i, "g"), replaces[i]);
		}
		return message;
	}


}
}
